package com.lanhai.common.file;

import java.util.zip.ZipEntry;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 文件解压缩
 */
public class UnzipFile {
    static final int BUFFER = 2048;

    public static void main(String argv[]) {
        String fileName = "/Users/ianzhang/test/eng_jars.zip";
        String outPath = "/Users/ianzhang/test/out/";
        UnzipFile.unzip(fileName,outPath);
    }

    public static void unzip(String fileName,String outPath) {
        try {
            ZipFile zipFile = new ZipFile(fileName);
            Enumeration emu = zipFile.entries();
            int i = 0;
            while (emu.hasMoreElements()) {
                ZipEntry entry = (ZipEntry)emu.nextElement(); //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
                if (entry.isDirectory()) {
                    new File(outPath + entry.getName()).mkdirs();
                    continue;
                }

                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                File file = new File(outPath + entry.getName());
                //
                File parent = file.getParentFile();
                if ( parent != null &&(!parent.exists())){
                    parent.mkdirs();
                }

                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
                int count;
                byte data[] = new byte[BUFFER];

                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    bos.write(data, 0, count);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
            zipFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}